//
//  CollectController.m
//  CarOfHome2
//
//  Created by 微 醺 on 15/9/30.
//  Copyright (c) 2015年 Frank. All rights reserved.
//

#import "CollectController.h"

#import "DataBaseHandle.h"

#import "WebViewController.h"

@interface CollectController () <UITableViewDataSource, UITableViewDelegate>

@end

@implementation CollectController

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBarHidden = YES;
    
    [super viewWillAppear:YES];
    
//    [self dataHandle];
    /* 没收藏的时候弹出来的 */
//    if (self.collectArr.count == 0) {
//        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"你还没收藏, 再去看看吧"preferredStyle:UIAlertControllerStyleAlert];
//        [self.tableView removeFromSuperview];
//        [self presentViewController:alert animated:YES completion:^{
//        }];
//        
//        UIAlertAction *action = [UIAlertAction actionWithTitle:@"返回" style:0 handler:^(UIAlertAction *action) {
//            
//            [self dismissViewControllerAnimated:YES completion:^{
//                
//            }];
//            
//        }];
//        [alert addAction:action];
//        
//    } else {
//        [self.tableView reloadData];
//    }
}


- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self headerViewSubView];
    
    [self dataHandle];
    
    [self tableViewSubView];
    
    [self viewShouldLoad];
}


#pragma mark -- 头部Viwe
- (void)headerViewSubView
{
    UIImageView *Image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"expert"]];
    Image.frame = CGRectMake(0, 0, SAWindth * 375, SAHeight * 64);
    [self.view addSubview:Image];
    UIButton *returnButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [returnButton setImage:[UIImage imageNamed:@"returnButton"] forState:UIControlStateNormal];
    returnButton.frame = CGRectMake(SAWindth * 10, SAHeight * 10, SAWindth * 50, SAHeight * 50);
    [self.view addSubview:returnButton];
    [returnButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UIButton *editingButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [editingButton setImage:[UIImage imageNamed:@"bianji"] forState:UIControlStateNormal];
    editingButton.frame = CGRectMake(SAWindth * 325, SAHeight * 10, SAWindth * 50, SAHeight * 50);
    [self.view addSubview:editingButton];
    [editingButton addTarget:self action:@selector(setTableViewEditng:) forControlEvents:UIControlEventTouchUpInside];
    
    
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(SAWindth * 135, SAHeight * 15, SAWindth * 100, SAHeight * 50)];
    title.text = @"我的收藏";
    title.textColor = [UIColor whiteColor];
    title.textAlignment = 1;
    //    title.center = Image.center;
    [Image addSubview:title];

}

/* 返回上一页 */
- (void)buttonClicked:(UIButton *)button
{
    [self dismissViewControllerAnimated:YES completion:^{
        
    }];
}

#pragma mark - 数据解析
- (void)dataHandle
{
    self.collectArr = [[[DataBaseHandle shareDataBaseHandle] selectViewWithType:@"首页"] mutableCopy];
    
    
    [self.tableView reloadData];
}

#pragma mark - 点击编辑按键方法

- (void)setTableViewEditng:(UIButton *)button
{


    button.selected = !button.selected;
    if (button.selected) {
        [self.tableView setEditing:YES animated:YES];
    }
    else
    {
        [self.tableView setEditing:NO animated:YES];
    }
}

#pragma mark - 删除方法
- (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    return YES;
}


- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        
        NSString *title = [[self.collectArr objectAtIndex:indexPath.row] objectForKey:@"title"];

        [[DataBaseHandle shareDataBaseHandle] deleteView:title];
        
        [self.collectArr removeObjectAtIndex:indexPath.row];
        
        [tableView reloadData];
    }
    
//    if (indexPath.section == 1) {
//        
//        
//        NSString *title = [[self.discountArr objectAtIndex:indexPath.row] objectForKey:@"title"];
//        NSLog(@"%@",title);
//        //        [[DataBaseHandle shareDataBaseHandle] open];
//        [[DataBaseHandle shareDataBaseHandle] deleteView:title];
//        
//        [self.discountArr removeObjectAtIndex:indexPath.row];
//        
//        
//        [tableView reloadData];
//    }
//    
    
    [self viewShouldLoad];
    
}




#pragma mark - tableView收藏视图加载
- (void)tableViewSubView;
{
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, SAWindth * 64, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - SAHeight * 64)];
    self.tableView.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"SCBJ"]];
    [self.view addSubview:self.tableView];
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.collectArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{

    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"reuse"];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"reuse"];
    }
    
    NSDictionary *dic = [self.collectArr objectAtIndex:indexPath.row];
    cell.textLabel.text = [dic objectForKey:@"title"];

    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return SAHeight * 50;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSDictionary *dic = [self.collectArr objectAtIndex:indexPath.row];
    
    WebViewController *web = [[WebViewController alloc] init];
    web.viewTitle = [dic objectForKey:@"title"];
    web.viewUrl = [dic objectForKey:@"url"];
    
    [self.navigationController pushViewController:web animated:YES];
    
    
}

#pragma mark - 是否加载
- (void)viewShouldLoad
{
    
    if (self.collectArr.count == 0) {
        
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"没有添加任何收藏" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
            
        }];
        
        [alert addAction:action];
        
        [self presentViewController:alert animated:YES completion:^{
            
            
        }];
    }
}


#pragma mark - Memory

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
